﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ShootSearch.Core;
using ShootSearch.Util.Serializers;
using System.IO;

namespace ShootSearch.Runner.Controls
{
    public class FormMainControl
    {
        FormMain form = null;
        ListView listViewTasks;
        ToolStripButton tsButtonRun;
        ToolStripButton tsButtonStop;
        //List<Task> taskManager = new List<Task>();
        Task task = null;

        public FormMainControl(FormMain form)
        {
            this.form = form;
            this.listViewTasks = form.listViewTasks;
            tsButtonRun = form.tsButtonRun;
            tsButtonStop = form.tsButtonStop;
        }

        public void LoadTasks()
        {
            string path = RunnerHelper.TaskPath;
            string[] taskfiles = Directory.GetFiles(path);
            foreach (string item in taskfiles)
            {
                TaskConfig tc = TaskConfig.Load(item);
                if (tc != null)
                {
                    AddTask(tc, item);
                }
            }
        }

        public void NewTask()
        {
            FormNewTask taskForm = new FormNewTask();
            if (taskForm.ShowDialog() == DialogResult.OK)
            {
                TaskConfig taskConfig =  taskForm.GetTask();
                XmlFormatter.ToXmlFile(taskConfig,taskForm.GetPath());
                AddTask(taskConfig, taskForm.GetPath());
            }
        }

        protected void AddTask(TaskConfig tc, string xmlPath)
        {
            if (tc != null)
            {
                Task newTask = new Task();
                newTask.Config = tc;
                //taskManager.Add(newTask);

                ListViewItem item = new ListViewItem(new string[]
                {
                    newTask.Config.TaskName,
                    xmlPath

                });
                item.ImageIndex = 0;
                item.Tag = newTask;

                listViewTasks.Items.Add(item);
            }
        }

        public void ItemActivate()
        {
            task = GetTask();
            if (task.Status == JobStatus.Running)
            {
                tsButtonRun.Enabled = false;
                tsButtonStop.Enabled = true;
            }
            else if (task.Status == JobStatus.Stopping ||
                task.Status == JobStatus.Completing)
            {
                tsButtonRun.Enabled = false;
                tsButtonStop.Enabled = false;
            }
            else if (task.Status == JobStatus.Unstarted)
            {
                tsButtonRun.Enabled = true;
                tsButtonStop.Enabled = false;
            }
        }

        public void TaskRun()
        {
            if (task != null)
                task.Start();
        }

        public void TaskStop()
        {
            if (task != null)
                task.Stop();
        }

        public Task GetTask()
        { 
            if (listViewTasks.SelectedItems.Count == 0) return null;
            ListViewItem item = listViewTasks.SelectedItems[0];
            Task task = item.Tag as Task;
            return task;

        }
    }
}
